﻿@page "/Admin/Notices"
@attribute [Authorize]
@attribute [TabItemOption(Text ="公告管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysNoticeService SysNoticeService
@inject ISysUserService SysUserService

<div class="overflow-hidden h-100">
    <Table TItem="SysNotice"
            @ref = "@_table"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="true"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowSearch="true"
            ShowEmpty="true" 
            ShowToolbar="true"
            ShowDefaultButtons="false"
            ShowExtendButtons ="true"
            SearchMode="SearchMode.Top"
            SearchModel="@_search"
            AddModalTitle="新增通知公告-草稿"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="195"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync"
            OnSaveAsync="@OnSaveAsync"
            OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Title" />
            <TableColumn @bind-Field="@context.Type" />
            <TableColumn @bind-Field="@context.Status" />
        </TableColumns>
        <DetailRowTemplate>
            <div class="container">
              <figure>
                  <blockquote class="blockquote">
                    <p>@context.Title</p>
                  </blockquote>
                  <figcaption class="blockquote-footer">
                    @context.PublicTime <cite title="@context.PublicUserName">@context.PublicUserName</cite>
                  </figcaption>
                </figure>
                @(new MarkupString($"<p>{context.Content}</p>"))
            </div>
        </DetailRowTemplate>
        <EditTemplate>
            <AsyncRender Context="val" OnQueryAsync="(()=> OnRenderAsync(context))">
                <div class="row g-3 form-inline">
                    <div class="col-sm-9">
                        <BootstrapInput @bind-Value="@context.Title" PlaceHolder="请输入标题" maxlength="50" />
                    </div>

                    <div class="col-sm-3">
                        <RadioList @bind-Value="@context.Type" class="item-w0" ShowLabel="false"/>
                    </div>

                    <div class="col-sm-12">
                        <div class="accordion" id="notice-edit-accordion">
                            <div class="accordion-item">
                                <h2 class="accordion-header" id="notice-edit-content-header">
                                    <button class="accordion-button" 
                                        type="button" 
                                        data-bs-toggle="collapse" 
                                        data-bs-target="#notice-edit-content-body" 
                                        aria-expanded="true" 
                                        aria-controls="notice-edit-content-body">
                                    内容
                                    </button>
                                </h2>
                                <div id="notice-edit-content-body" 
                                    class="accordion-collapse collapse show" 
                                    aria-labelledby="notice-edit-content-header" 
                                    data-bs-parent="#notice-edit-accordion">
                                    <div class="accordion-body">
                                        <Markdown @bind-Value="@context.Content" PreviewStyle="PreviewStyle.Tab" InitialEditType="InitialEditType.Wysiwyg"/>
                                    </div>
                                </div>
                            </div>

                            <div class="accordion-item">
                                <h2 class="accordion-header" id="notice-edit-users-header">
                                    <button class="accordion-button collapsed" 
                                        type="button" 
                                        data-bs-toggle="collapse" 
                                        data-bs-target="#notice-edit-users-body" 
                                        aria-expanded="false" 
                                        aria-controls="notice-edit-users-body">
                                    通知到的人
                                    </button>
                                </h2>
                                <div id="notice-edit-users-body" 
                                    class="accordion-collapse collapse" 
                                    aria-labelledby="notice-edit-users-header" 
                                    data-bs-parent="#notice-edit-accordion">
                                    <div class="accordion-body">
                                    <Transfer @bind-Value="@context.NoticeUserIds" 
                                                LeftPannelSearchPlaceHolderString ="请输入搜索内容"
                                                RightPannelSearchPlaceHolderString ="请输入搜索内容"
                                                ShowSearch="true" 
                                                ShowLabel="false" 
                                                Items="@_users"/></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </AsyncRender>
        </EditTemplate>
        <TableToolbarTemplate>
            <TableToolbarButton Color="Color.Success" OnClick="_table!.AddAsync" Icon="fa fa-plus" Text="新增公告" /> 
        </TableToolbarTemplate>
        <RowButtonTemplate>
            @if (context.Status == NoticeStatus.DRAFT)
            {
            <Button Size="Size.ExtraSmall" OnClick="() => ShowEditFormAsync(context)" Icon="fa fa-edit" Text="编辑" />    
            <Button Size="Size.ExtraSmall" OnClick="() => OnUpdateAsync(context, NoticeStatus.PUBLIC)" Icon="fa fa-edit" Text="发布" />  
            }
            else if(context.Status == NoticeStatus.PUBLIC)
            {
                <Button Size="Size.ExtraSmall" OnClick="() => OnUpdateAsync(context, NoticeStatus.CANCEL)" Icon="fa fa-times" Text="撤回" />   
            }
            <Button Size="Size.ExtraSmall" OnClick="() => OnUpdateAsync(context, NoticeStatus.DELETED)" Icon="fa fa-times" Text="删除" />  
        </RowButtonTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-sm-3">
                        <BootstrapInput @bind-Value="@context.SearchValue" ShowLabel="true"/>
                    </div>
                    <div class="col-sm-3">
                        <RadioList @bind-Value="@context.Type" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

@code{
    private Table<SysNotice>? _table;
    private SysNotice _search = new();
    private IEnumerable<SelectedItem>? _users;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            _users = await SysUserService.SelectorAsync(new());
        }
    }

    private async Task<SysNotice> OnRenderAsync(SysNotice context)
    {
        if (context.Id > 0)
        {
            var data = await SysNoticeService.DetailAsync(context);
            if (data != null)
            {
                context.NoticeUserIdList = data.NoticeUserIdList;
                context.NoticeUserReadInfoList = data.NoticeUserReadInfoList;
            }
        }
        return context;
    }

    /// <summary>
    /// 弹出编辑窗口
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    private async Task ShowEditFormAsync(SysNotice item)
    {
        _table!.SelectedRows.Clear();
        _table!.SelectedRows.Add(item);

        // 更新行选中状态
        await _table!.EditAsync();
    }

    /// <summary>
    /// 更新、删除、撤回操作
    /// </summary>
    /// <param name="item"></param>
    /// <param name="status"></param>
    /// <returns></returns>
    private async Task OnUpdateAsync(SysNotice item, NoticeStatus status)
    {
        await _table!.ToggleLoading(true);
        item.Status = status;
        if (await SysNoticeService.ChangeStatusAsync(item))
        {
            _table!.SelectedRows.Clear();
            await _table!.QueryAsync();
        }
        await _table!.ToggleLoading(false);
    }

    private Task<QueryData<SysNotice>> OnQueryAsync(QueryPageOptions options)
        => SysNoticeService.PageAsync(_search.Adapt<NoticePageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<SysNotice> items) 
        => SysNoticeService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(SysNotice item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysNoticeService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysNoticeService.EditAsync(item);
                break;
        }
        return result;
    }
}